import { reactive, inject } from "vue";
import { useLoginUserStore } from "../../../store";
import { useOrgStore } from '../../../store/useOrgStore'
import { useUserStore } from '../../../store/useUserStore'

export const useUserTable = (currentPage: any, pageSize: any) => {
    //获取API配置
    const API = inject("API") as any;
    const ApiConfig = inject("ApiConfig") as any;
    const userTable = reactive(
        {
            total: 0,
            tableData: [],
            queryCondition: {
                realName: '',
                orgId: '',
                orgName: '',
                searchAll: false
            },
            roles: []
        }
    );

    const loginUserStore = useLoginUserStore();
    const orgStore = useOrgStore();
    const userStore = useUserStore();
    userTable.queryCondition.orgId = orgStore.selectedOrgNode == null ? loginUserStore.currentUser.orgId : orgStore.selectedOrgNode.id;

    //查询
    const queryData = async () => {
        try {
            const response = await API.request(
                ApiConfig.Methods.POST,
                ApiConfig.PATH.User.GetUserByOrgId,
                {
                    pageIndex: currentPage.value,
                    pageSize: pageSize.value,
                    condition: { RealName: userTable.queryCondition.realName, orgId: userTable.queryCondition.orgId, searchAll: userTable.queryCondition.searchAll },
                }
            );
            if (response.status == "200" && response.data.code == "200") {
                userTable.tableData = response.data.data;
                userTable.total = response.data.count;
            }
        } catch (error) {
            console.error(error);
        }
    };
    //新增
    const addData = async () => {
        try {
            const response = await API.request(
                ApiConfig.Methods.POST,
                ApiConfig.PATH.User.Create,
                userStore.userModel
            );
            if (response.status == "200" && response.data.code == "200") {
                return true;
            }
        } catch (error) {
            console.error(error);
        }
        return false;
    }
    //更新
    const updateData = async () => {
        try {
            const response = await API.request(
                ApiConfig.Methods.POST,
                ApiConfig.PATH.User.Update,
                userStore.userModel
            );
            if (response.status == "200" && response.data.code == "200") {
                return true;
            }
        } catch (error) {
            console.error(error);
        }
        return false;
    }
    //删除
    const deleteData = async (userId) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.GET,
                ApiConfig.PATH.User.DeleteByUserId,
                { 'userId': userId }
            );
            if (response.status == "200" && response.data.code == "200") {
                return true;
            }
        } catch (error) {
            console.error(error);
        }
        return false;
    }
    //配置用户角色
    const configureUserRoles = async (userId, roles) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.GET,
                ApiConfig.PATH.User.ConfigureUserRoles,
                { userId: userId, roles: roles }
            );
            if (response.status == "200" && response.data.code == "200") {
                return true;
            }
        } catch (error) {
            console.error(error);
        }
        return false;
    }
    //获取用户角色
    const getUserRoles = async (userId) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.GET,
                ApiConfig.PATH.User.GetUserRolesByUserId,
                {
                    userId: userId
                }
            );
            if (response.status == "200" && response.data.code == "200") {
                userTable.roles = response.data.data;
            }
        } catch (error) {
            console.error(error);
        }
    }

    return { userTable, userStore, queryData, addData, updateData, deleteData, configureUserRoles, getUserRoles };
};